package com.pandy.code;

/**
 * 链表相关
 */
public class Code04_List {

    /**
     * 定义一个单链表
     */
    static class SinglyNode {
        private Integer value;
        private SinglyNode next;

        public SinglyNode(Integer value) {
            this.value = value;
        }
    }

    class DoublyNode {
        private Integer value;

        private DoublyNode left;
        private DoublyNode right;

        public DoublyNode(Integer value) {
            this.value = value;
        }
    }

    /**
     * 单链表反转
     * @param head
     * @return
     */
    public static SinglyNode singlyNodeReversal(SinglyNode head) {
        SinglyNode pre = null;
        SinglyNode next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

    public static void main(String[] args) {
        SinglyNode s1 = new SinglyNode(1);
        SinglyNode s2 = new SinglyNode(2);
        SinglyNode s3 = new SinglyNode(3);
        SinglyNode s4 = new SinglyNode(4);
        SinglyNode s5 = new SinglyNode(5);
        s1.next = s2;
        s2.next = s3;
        s3.next = s4;
        s4.next = s5;
        s5.next = null;

        System.out.println(singlyNodeReversal(s1).value);
    }
}
